import { getSecond , uuid} from '../../utils/common'
import { Account , Token , AccountDetail , Address } from '../../models/'
import { CODE , TOKEN_TIME } from '../../config/config'

/**
 * @api {post} /address/getAddress 获取用户地址信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getAddress
 * @apiGroup address
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/address/getAddress'
 * @apiParam {String}  token                 用户token
 * 
 * @apiSuccess {Array}    addresses            地址集合
 * @apiSuccess {Object}   address              地址信息
 * @apiSuccess {String}   id                   地址id
 * @apiSuccess {String}   uid                  创建地址用户id
 * @apiSuccess {String}   address              具体地址
 * @apiSuccess {String}   province             省
 * @apiSuccess {String}   city                 市
 * @apiSuccess {String}   person               收件人姓名
 * @apiSuccess {String}   phone                收件人手机号
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/address/getAddress
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getAddress(ctx) {
	const token      = ctx.body.token
	const phone_uid  = token.phone_uid
	const wechat_uid = token.wechat_uid
	const qq_uid     = token.qq_uid

	const query      = {status:1}
	const uid        = []
	if(phone_uid) {
		uid[uid.length] = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length] = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length] = qq_uid
	}
	query.uid = uid
	const addresses  = await Address.findAll({where:query})
	ctx.body         = CODE.success({addresses})
}

/**
 * @api {post} /address/updateAddress 修改用户地址信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName updateAddress
 * @apiGroup address
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/address/updateAddress'
 * @apiParam {String}  token                   用户token
 * @apiParam {String}  id                      地址id
 * @apiParam {String}   address                具体地址
 * @apiParam {String}   province               省
 * @apiParam {String}   city                   市
 * @apiParam {String}   person                 收件人姓名
 * @apiParam {String}   phone                  收件人手机号
 * @apiParam {String}   ext                    拓展字段
 * 
 * @apiSuccess {Array}    addresses            地址集合
 * @apiSuccess {Object}   address              地址信息
 * @apiSuccess {String}   id                   地址id
 * @apiSuccess {String}   uid                  创建地址用户id
 * @apiSuccess {String}   address              具体地址
 * @apiSuccess {String}   province             省
 * @apiSuccess {String}   city                 市
 * @apiSuccess {String}   person               收件人姓名
 * @apiSuccess {String}   phone                收件人手机号
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/address/updateAddress
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function updateAddress(ctx) {
	const token      = ctx.body.token
	const id         = ctx.request.body.id
	const phone_uid  = token.phone_uid
	const wechat_uid = token.wechat_uid
	const qq_uid     = token.qq_uid
	const uid        = []
	if(phone_uid) {
		uid[uid.length] = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length] = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length] = qq_uid
	}
	const address    = await Address.findOne({where:{uid,id,status:1}})
	if(address) {
		address.address    = ctx.request.body.address || address.address
		address.province   = ctx.request.body.province || address.province
		address.city       = ctx.request.body.city || address.city
		address.phone      = ctx.request.body.phone || address.phone
		address.person     = ctx.request.body.person || address.person
		address.ext        = ctx.request.body.ext || address.ext
		await address.save()
		ctx.body           = CODE.success({address})
		return
	}
	
	ctx.body         = CODE.ERROR.ERROR_ADDRESS_NOT_FOUNT

}



/**
 * @api {post} /address/deleteAddress 删除用户地址信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName deleteAddress
 * @apiGroup address
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/address/deleteAddress'
 * @apiParam {String}  token                   用户token
 * @apiParam {String}  id                      地址id
 * @apiSampleRequest http://copyright.hotchain.vip/api/address/deleteAddress
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function deleteAddress(ctx) {
	const token      = ctx.body.token
	const id         = ctx.request.body.id
	const phone_uid  = token.phone_uid
	const wechat_uid = token.wechat_uid
	const qq_uid     = token.qq_uid
	const uid        = []
	if(phone_uid) {
		uid[uid.length] = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length] = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length] = qq_uid
	}
	const address    = await Address.findOne({where:{uid,id,status:1}})
	if(address) {
		address.status    = 2
		await address.save()
		ctx.body           = CODE.success()
		return
	}
	
	ctx.body         = CODE.ERROR.ERROR_ADDRESS_NOT_FOUNT

}



/**
 * @api {post} /address/addAddress 新增用户地址信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName addAddress
 * @apiGroup address
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/address/addAddress'
 * @apiParam {String}  token                   用户token
 * @apiParam {String}  id                      地址id
 * @apiParam {String}   address                具体地址
 * @apiParam {String}   province               省
 * @apiParam {String}   city                   市
 * @apiParam {String}   person                 收件人姓名
 * @apiParam {String}   phone                  收件人手机号
 * @apiParam {String}   ext                    拓展字段
 * 
 * @apiSuccess {Object}   address              地址信息
 * @apiSuccess {String}   id                   地址id
 * @apiSuccess {String}   uid                  创建地址用户id
 * @apiSuccess {String}   address              具体地址
 * @apiSuccess {String}   province             省
 * @apiSuccess {String}   city                 市
 * @apiSuccess {String}   person               收件人姓名
 * @apiSuccess {String}   phone                收件人手机号
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/address/addAddress
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function addAddress(ctx) {
	const token      = ctx.body.token
	const uid        = token.phone_uid || token.wechat_uid || token.qq_uid

	const address    = ctx.request.body.address
	const province   = ctx.request.body.province
	const city       = ctx.request.body.city
	const phone      = ctx.request.body.phone
	const person     = ctx.request.body.person
	const ext        = ctx.request.body.ext
	const ctime      = getSecond()

	const addr       = await Address.create({uid,address,province,city,phone,person,ctime,ext})
	
	ctx.body         = CODE.success({address:addr})

}

export async function  checkToken(ctx,next) {
	const token 	  = ctx.request.body.token
	const tokenRes    = await Token.findOne({where:{token,status:1}})
	if(tokenRes) {
		if(tokenRes.ctime + TOKEN_TIME >= getSecond()) {
			ctx.body  = {token:tokenRes}
			if(next) {
				return next()
			}
		}
		ctx.body      = CODE.ERROR.ERROR_TOKEN_LOSE
		return
	}
	ctx.body          = CODE.ERROR.ERROR_TOKEN_NOT_CHECK
}


/**
 * @apiDefine ERROR
 * @apiErrorExample {json} 错误返回:
 *     {
 *       "code": "错误码",
 *       "error": "错误描述",
 *       "msg":"错误信息"
 *     }
 */